package com.sensePlusVR.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;

import com.sensePlusVR.utils.MyLog;

import java.util.ArrayList;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:51:48
 * @desc:Home中三个Fragment管理 Adapter 
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
	private static final String TAG = MyFragmentPagerAdapter.class.getSimpleName();
	private Fragment mCurrentFragment;  
    private ArrayList<Fragment> fragmentsList;
	private FragmentManager fm;

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        this.fragmentsList = fragments;
        this.fm = fm;
    }

    @Override
    public int getCount() {
        return fragmentsList == null ? 0 : fragmentsList.size();
    } 

    @Override
    public Fragment getItem(int position) {
    	MyLog.d(TAG, "getItem fragment : " + fragmentsList.get(position));
        return fragmentsList.get(position);
    }
    
	@Override
	public long getItemId(int position) {
		MyLog.d(TAG, "getItemId position : " + position);
		return super.getItemId(position);
	}
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		MyLog.d(TAG, "instantiateItem : " + position);
		return super.instantiateItem(container, position);
	}
	
	@Override
	public int getItemPosition(Object object) {
		MyLog.d(TAG, "getItemPosition : " + object.toString());
		return POSITION_NONE;
	}
	
	
	public void setFragments(ArrayList fragments) {
		   if(this.fragmentsList != null){
		      FragmentTransaction ft = fm.beginTransaction();
		      for(Fragment f:this.fragmentsList){
		        ft.remove(f);
		      }
		      ft.commit();
		      ft=null;
		      fm.executePendingTransactions();
		   }
		  this.fragmentsList = fragments;
		  notifyDataSetChanged();
		}
	
	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		mCurrentFragment = (Fragment) object;  
		super.setPrimaryItem(container, position, object);
	}
	
	 public Fragment getCurrentFragment() {  
	        return mCurrentFragment;  
	    } 
}
